package com.atguigu.gulimall.utils;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPCellEvent;
import com.itextpdf.text.pdf.PdfPTable;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public abstract class PdfFilePathUtil {
    protected static String pdfPath;
    private String fileName;

    public PdfFilePathUtil() {}

    public PdfFilePathUtil(String pdfPath) {
        // 1. 生成PDF的基礎路勁
        this.pdfPath = pdfPath;
        // 2. 如果這個路勁不存在則創建
        FileUtil.createDirectory(pdfPath);
    }

    public String getFileName() {
        return fileName;
    }
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    /**
     * 文件名后缀的文件
     * @param nameSuffix
     * @return
     */
    public abstract String file(String nameSuffix);

    //虚线格式 顶部
    static class CustomCellTOP implements PdfPCellEvent {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            // TODO Auto-generated method stub
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.5f);
            cb.setLineDash(new float[]{3.0f, 3.0f}, 0);
            cb.moveTo(position.getLeft(), position.getTop());
            cb.lineTo(position.getRight(), position.getTop());
            cb.stroke();
            cb.restoreState();


        }
    }

    //虚线格式 右边
    static class CustomCellRight implements PdfPCellEvent {
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.5f);
            cb.setLineDash(new float[] { 3.0f, 3.0f }, 0);
            cb.moveTo(position.getRight(), position.getTop());
            cb.lineTo(position.getRight(),position.getBottom());
            cb.stroke();
            cb.restoreState();


        }
    }

    // 虛線格式  左邊
    static class CustomCellLeft implements PdfPCellEvent {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.5f);
            cb.setLineDash(new float[] {3.0f, 3.0f},0);
            cb.moveTo(position.getLeft(),position.getTop());
            cb.lineTo(position.getLeft(),position.getBottom());
            cb.stroke();
            cb.restoreState();


        }
    }

    //虚线格式 底部
    static class CustomCellBottom implements PdfPCellEvent {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            // TODO Auto-generated method stub
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.5f);
            cb.setLineDash(new float[]{3.0f, 3.0f}, 0);
            cb.moveTo(position.getLeft(), position.getBottom());
            cb.lineTo(position.getRight(), position.getBottom());
            cb.stroke();
            cb.restoreState();
        }
    }

    //左
    PdfPCellEvent lineLeft = new PdfPCellEvent() {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.1f);
            cb.setColorStroke(new BaseColor(200,200,200));
            cb.setLineDash(new float[] { 1.0f, 1.0f }, 0);
            cb.moveTo(position.getLeft(), position.getTop());
            cb.lineTo(position.getLeft(),position.getBottom());
            cb.stroke();
            cb.restoreState();
        }
    };

    //右
    PdfPCellEvent lineRight=new PdfPCellEvent() {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.1f);
            cb.setColorStroke(new BaseColor(200,200,200));
            cb.setLineDash(new float[] { 1.0f, 1.0f }, 0);
            cb.moveTo(position.getRight(), position.getTop());
            cb.lineTo(position.getRight(),position.getBottom());
            cb.stroke();
            cb.restoreState();
        }
    };
    //上
    PdfPCellEvent lineTop=new PdfPCellEvent() {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.1f);
            cb.setColorStroke(new BaseColor(200,200,200));
            cb.setLineDash(new float[] { 1.0f, 1.0f }, 0);
            cb.moveTo(position.getLeft(), position.getTop());
            cb.lineTo(position.getRight(), position.getTop());
            cb.stroke();
            cb.restoreState();
        }
    };
    //下
    PdfPCellEvent lineBottom=new PdfPCellEvent() {
        @Override
        public void cellLayout(PdfPCell cell, Rectangle position, PdfContentByte[] canvases) {
            PdfContentByte cb = canvases[PdfPTable.LINECANVAS];
            cb.saveState();
            cb.setLineWidth(0.1f);
            cb.setColorStroke(new BaseColor(200,200,200));
            cb.setLineDash(new float[] { 1.0f, 1.0f }, 0);
            cb.moveTo(position.getLeft(), position.getBottom());
            cb.lineTo(position.getRight(), position.getBottom());
            cb.stroke();
            cb.restoreState();
        }
    };

    //add虚线
    public void addPdfDottedLine(PdfPCell pdfPCell, int type) {

        pdfPCell.setBorder(Rectangle.NO_BORDER);
//        pdfPCell.setCellEvent(lineLeft);// 左
//        pdfPCell.setCellEvent(lineRight); // 右
//        pdfPCell.setCellEvent(lineTop); // 上
//        pdfPCell.setCellEvent(lineBottom); // 下

        switch (type) {
            case 0:
                pdfPCell.setCellEvent(lineLeft); // 左
                pdfPCell.setCellEvent(lineRight); // 右
                pdfPCell.setCellEvent(lineTop); // 上
                pdfPCell.setCellEvent(lineBottom); // 下
                break;
            case 1:
                pdfPCell.setCellEvent(lineTop); // 上
                break;
            case 2:
                pdfPCell.setCellEvent(lineBottom); // 下
                break;
            case 3:
                pdfPCell.setCellEvent(lineLeft); // 左
                break;
            case 4:
                pdfPCell.setCellEvent(lineRight); // 右
                break;
            case 5:
                pdfPCell.setCellEvent(lineTop); // 上
                pdfPCell.setCellEvent(lineBottom); // 下
                break;
            case 6:
                pdfPCell.setCellEvent(lineTop); // 上
                pdfPCell.setCellEvent(lineLeft); // 左
                break;
            case 7:
                pdfPCell.setCellEvent(lineTop); // 上
                pdfPCell.setCellEvent(lineRight); // 右
            case 8:
                pdfPCell.setCellEvent(lineBottom); // 下
                pdfPCell.setCellEvent(lineLeft); // 左
                break;
            case 9:
                pdfPCell.setCellEvent(lineBottom); // 下
                pdfPCell.setCellEvent(lineRight); // 右
                break;
            case 10:
                pdfPCell.setCellEvent(lineLeft); // 左
                pdfPCell.setCellEvent(lineRight); // 右
                break;
            default:
                pdfPCell.setBorder(Rectangle.NO_BORDER);
                break;
        }
    }

    /**
     *
     * @param inputStream
     * @return 将InputStream转化为Image
     * @throws Exception
     */
    public static Image getImageFromInputStream(InputStream inputStream) throws Exception {
        int n;
        byte[] buffer = new byte[4096];
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while (-1 != (n = inputStream.read(buffer))) {
            output.write(buffer, 0, n);
        }
        Image image = Image.getInstance(output.toByteArray());
        return image;
    }
}
